package carcassonne.server.ioc;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public abstract class AbsMessageDispatcher {

    //保存需要注入的类的缓存
    protected List<Object> Controllers = new ArrayList<>();

    // 完整路径和 方法的 mapping
    protected Map<String, Method> WebsocketFunctions = new ConcurrentHashMap<>();
    protected Map<String, MethodInfo> GetFunctions = new ConcurrentHashMap<>();
    protected Map<String, MethodInfo> PostFunctions = new ConcurrentHashMap<>();
    protected Map<String, MethodInfo> PutFunctions = new ConcurrentHashMap<>();
    protected Map<String, MethodInfo> DeleteFunctions = new ConcurrentHashMap<>();

    // 类路径和controller 的 mapping
    public Map<String, Object> controllerMapping = new ConcurrentHashMap<>();

    protected AbsMessageDispatcher(List<Object> controllers) {
//        ScanPackage(path);
        Controllers = controllers;
        MappingMVC();
    }

    protected void ScanPackage(ConcurrentHashMap<String, Object> controllers) {

//        URL url = this.getClass().getClassLoader().getResource(path.replaceAll("\\.", "/"));
//        if (url == null) return;
////        System.out.println("scanPackage:" + url.getPath());
//        try {
//            File file = new File(url.toURI());
//
//            File[] files = file.listFiles(new FileFilter() {
//                //pathname 表示当前目录下的所有文件
//                @Override
//                public boolean accept(File pathname) {
//                    //递归查找文件
//                    if (pathname.isDirectory()) {
//                        ScanPackage(path + "." + pathname.getName());
//                    } else {
//                        if (pathname.getName().endsWith(".class")) {
//                            String classPath = path + "." + pathname.getName().replace(".class", "");
////                            System.out.println("addClassPath:" + classPath);
////                            classCache.add(classPath);
//                            try {
//                                Class<?> clazz = Class.forName(classPath);
//                                if (clazz.isAnnotationPresent(Controller.class)) {
//                                    Object instance = clazz.getDeclaredConstructor().newInstance();
//                                    Controllers.add(instance);
//                                }
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
//                        }
//                    }
//                    return true;
//                }
//            });
//        } catch (URISyntaxException e) {
//            e.printStackTrace();
//        }
    }

    protected abstract void MappingMVC();
}
